// Select.cpp: implementation of the CSelect class.
// Writen by SpiritLM, Northwestern Polytechnical University
// Date: June, 2007
//////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include <stdio.h> 
#include <uf.h>
#include <uf_defs.h>
#include <uf_ui.h>
#include <uf_styler.h>
#include <uf_mb.h>
#include "Select.h"
#include <uf_curve.h>
#include <uf_modl.h>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSelect::CSelect()
{
	strTitle = "Select Objects";
}

CSelect::~CSelect()
{

}

int CSelect::SelectPoint(tag_t *selected)
{
	int flag;
	flag = SelectPCSB(selected, 1);
	return flag;
}

int CSelect::SelectCurve(tag_t *selected)
{
	int flag;
	flag = SelectPCSB(selected, 2);
	return flag;
}

int CSelect::SelectSurface(tag_t *selected)
{
	int flag;
	flag = SelectPCSB(selected, 3);
	return flag;
}

int CSelect::SelectBody(tag_t *selected)
{
	int flag;
	flag = SelectPCSB(selected, 4);
	return flag;
}

void CSelect::SetPrompt(char * strPrompt)
{
	strTitle = strPrompt;
}

int CSelect::SelectPCSB(tag_t *selected, int type)
{
	int irc = 0;
	char *message;
	UF_UI_selection_options_t opts;
	UF_UI_mask_t mask;
	int response;
	tag_t object, view;
	double cursor[3];
	char error[133];
	int unhighlight;
    
	*selected = 0;
	unhighlight = 0;
	
	opts.other_options = 0;
	opts.reserved = NULL;
	opts.num_mask_triples = 1;
	opts.mask_triples = &mask;

	message = strTitle;
	
	switch( type )
	{
	case 1:
// 		message = "Select point.";
		opts.mask_triples->object_type = UF_point_type ;  
		opts.mask_triples->object_subtype = UF_all_subtype ;
		opts.mask_triples->solid_type = UF_UI_SEL_NOT_A_FEATURE ;
		break;
	case 2:
// 		message = "Select curve.";
		opts.mask_triples->object_type = UF_spline_type ;  
		opts.mask_triples->object_subtype = UF_all_subtype ;
		opts.mask_triples->solid_type = UF_UI_SEL_NOT_A_FEATURE ;
		break;
	case 3:
// 		message = "Select surface.";
		opts.mask_triples->object_type =  UF_solid_type;  
		opts.mask_triples->object_subtype = 0 ;
		opts.mask_triples->solid_type = UF_UI_SEL_FEATURE_ANY_FACE ;
		break;
	case 4:
// 		message = "Select body.";
		opts.mask_triples->object_type =  UF_solid_type;  
		opts.mask_triples->object_subtype = UF_all_subtype;
		opts.mask_triples->solid_type = UF_UI_SEL_FEATURE_SOLID_BODY;
		break;
	default:break;
	}
	opts.scope = UF_UI_SEL_SCOPE_WORK_PART;
	irc = UF_UI_select_single(message,&opts,&response,
							  &object,cursor,&view);

	if (!irc)
	{
	  printf("response= %d, object tag= %d, view tag= %d, cursor position= %f %f %f\n"
			 ,response, object, view, cursor[0], cursor[1], cursor[2]);
  
	  *selected = object;
	  /* unhighlight selected object */
	  UF_DISP_set_highlight(object, unhighlight);
	}
	else  /* else get error message */
	{
	  UF_get_fail_message(irc, error);
	  printf("ERROR: %s\n", error);
	}
	/*   UF_terminate();   */

	if(*selected == 0) 
		return 0;
	return 1;
}